import random
import uuid
from Autotests.Suites.StressTest.Operations.CommonOperations import CommonOperations
from Config.RunConfig import RunConfig, Options


class AccountOperations:
    def __init__(self):
        self._operationsList = [
            self._addAccount,
            self._addAccountGroup,
            self._changeAccountPassword,
            self._enumerateAccounts,
            self._enumerateAccountGroups,
            self._enumerateAccountHistoryStatistics,
            self._enumerateAccountStatistics,
            self._enumerateAccountStatisticsReports,
            self._getAccountCustomColumnValues,
            self._getAccountFeatures,
            self._getAccountFeaturesById,
            self._getAccountGroup,
            self._getAccountInfo,
            self._getAccountInfoById,
        ]
        if RunConfig().get(Options.StressTest.stressAllPartners) == 'True':
            self._operationsList.append(self._modifyAccount)
            self._operationsList.append(self._modifyAccountById)
            self._operationsList.append(self._removeAccountById)

        self._baseAccount = {
            'partnerId': 0,
            'productId': 0,
            'locationId': 0,
            'name': 'stress123456789',
            'password': '123456789'
        }

    def _getRandomOperation(self):
        return random.choice(self._operationsList)

    def _addAccount(self, manserv, partnerID):
        level = manserv.GetPartnerInfoById(id = partnerID).level
        if level != 'SubRoot':
            result = manserv.EnumerateProducts(partnerId = partnerID)
            if len(result.products.productInfo) > 1:
                randomProduct = random.randint(0, len(result.products.productInfo) - 1)
                product = result.products.productInfo[randomProduct]
                productId = product.id

                locationResult = manserv.EnumerateLocations()
                if len(locationResult.locationInfo) > 1:
                    randomLocation = random.randint(0, len(locationResult.locationInfo) - 1)
                    locationId = locationResult.locationInfo[randomLocation].id
                    self._baseAccount['partnerId'] = partnerID
                    self._baseAccount['productId'] = productId
                    self._baseAccount['locationId'] = locationId
                    if random.choice([True, False]):
                        self._baseAccount['name'] = uuid.uuid4()
                        self._baseAccount['password'] = uuid.uuid4()
                    manserv.AddAccount(**self._baseAccount)


    def _changeAccountPassword(self, manserv, partnerID):
        manserv.ChangeAccountPassword(account = self._baseAccount['name'],
                                      oldPassowrd = self._baseAccount['password'],
                                      newPassword = self._baseAccount['password'])

    def _addAccountGroup(self, manserv, partnerID):
        manserv.AddAccountGroup(id = random.randint(1, 10), name = uuid.uuid4(), partnerID = partnerID)

    def _enumerateAccountGroups(self, manserv, partnerId):
        manserv.EnumerateAccountGroups(partnerId = partnerId)

    def _enumerateAccounts(self, manserv, partnerId):
        manserv.EnumerateAccounts(partnerId = partnerId)

    def _getAccountCustomColumnValues(self, manserv, partnerId):
        result = manserv.EnumerateAccounts(partnerId = partnerId)
        if result:
            manserv.GetAccountCustomColumnValues(accountName = self._baseAccount['name'],
                                                 password = self._baseAccount['password'])

    def _getAccountCustomColumnValueById(self, manserv, partnerId):
        manserv.GetAccountCustomColumnValuesById(accountId = random.randint(1, 1000))

    def _enumerateAccountHistoryStatistics(self, manserv, partnerId):
        manserv.EnumerateAccountHistoryStatistics(partnerId = partnerId, startRecordNumber = random.randint(100),
            recordsCount = random.randint(1000), timeslice = '2034-04-17T16:50:45')

    def _enumerateAccountStatistics(self, manserv, partnerId):
            manserv.EnumerateAccountStatistics(partnerId = partnerId, startRecordNumber = random.randint(100),
                recordsCount = random.randint(1000))

    def _enumerateAccountStatisticsReports(self, manserv, partnerId):
        manserv.EnumerateAccountStatisticsReports(format = 'Ascii', granularity = 'AllReportsTogether',
            partnerId = partnerId, timestamp = '2034-04-17T16:50:45', startRecordNumber = random.randint(100),
            recordsCount = random.randint(1000))

    def _getAccountFeatures(self, manserv, partnerId):
        manserv.GetAccountFeatures(account = self._baseAccount['name'], passowrd = self._baseAccount['password'])

    def _getAccountFeaturesById(self, manserv, partnerId):
        manserv.GetAccountFeaturesById(accountId = random.randint(1, 1000))

    def _getAccountGroup(self, manserv, partnerId):
        manserv.GetAccountGroup(id = random.randint(1, 1000))

    def _getAccountInfo(self, manserv, partnerId):
        return manserv.GetAccountInfo(account = self._baseAccount['name'], passowrd = self._baseAccount['password'])

    def _getAccountInfoById(self, manserv, partnerId):
        return manserv.GetAccountInfoById(accountId = random.randint(1, 1000),
                                          includeHomeNodeInfo = random.choice([True, False]))

    def _modifyAccount(self, manserv, partnerId):
        account = self._getAccountInfoById(self, manserv, partnerId)
        manserv.ModifyAccount(name = account.name, password = account.password, partnerId = random.randint(1, 1000))

    def _modifyAccountById(self, manserv, partnerId):
        account = self._getAccountInfoById(self, manserv, partnerId)
        manserv.ModifyAccountById(id = account.id, name = account.name, password = account.password,
                                  partnerId = random.randint(1, 1000))

    def _removeAccountById(self, manserv, partnerId):
        id = random.randint(1, 1000)
        if id % CommonOperations()._baseGrowthRate == 0:
            return manserv.RemoveAccountById(accountId = random.randint(1, 1000))
